import 'package:flutter/services.dart';

typedef AdCallback = void Function();
typedef AdCallbackShow = void Function(int eCPM);
typedef AdRewardVideoCallbackShow = void Function(int eCPM, String orderNo);
typedef AdErrorCallback = void Function(String message);

class TbAd {
  final methodChannel = const MethodChannel('com.tb.mob');
  final EventChannel eventChannel = const EventChannel("com.tb.mob/event_");

  //'1330509223779385377-7'
  @override
  void initAd(String initId,
      {AdCallback? onSuccess,
      AdErrorCallback? onFail}) {
    eventChannel.receiveBroadcastStream().listen((event) {
      switch (event["event"]) {
        case "onSuccess":
          onSuccess?.call();
          break;
        case "onFail":
          onFail?.call(event["message"]);
          break;
      }
    });
    methodChannel.invokeMethod<String>('init', {"codeId": initId});
    /*Future.delayed(const Duration(milliseconds: 100), () {
    });*/
  }

  @override
  void requestPermissionIfNecessary() {
    methodChannel.invokeMethod<String>('requestPermission', {"": ""});
  }

  //'1330512946933022784'
  @override
  void loadSplash(String codeId,
      {AdCallbackShow? onAdShow,
      AdCallback? onAdClick,
      AdCallback? onAdClose,
      AdErrorCallback? onError}) {
    eventChannel.receiveBroadcastStream().listen((event) {
      switch (event["event"]) {
        case "onAdShow":
          onAdShow?.call(event["eCPM"]);
          break;
        case "onAdClick":
          onAdClick?.call();
          break;
        case "onAdClose":
          onAdClose?.call();
          break;
        case "onError":
          onError?.call(event["message"]);
          break;
      }
    });
    methodChannel.invokeMethod<String>('loadSplash', {"codeId": codeId});
  }

  //'1330513058157576258'
  @override
  void loadInteraction(String codeId,
      {AdCallbackShow? onAdShow,
      AdCallback? onAdClick,
      AdCallback? onAdClose,
      AdErrorCallback? onError}) {
    eventChannel.receiveBroadcastStream().listen((event) {
      switch (event["event"]) {
        case "onAdShow":
          onAdShow?.call(event["eCPM"]);
          break;
        case "onAdClick":
          onAdClick?.call();
          break;
        case "onAdClose":
          onAdClose?.call();
          break;
        case "onError":
          onError?.call(event["message"]);
          break;
      }
    });
    methodChannel.invokeMethod<String>('loadInteraction', {"codeId": codeId});
  }

  //'1330515047692124225'
  @override
  void loadRewardVideo(String codeId, String userId, String extra,
      {AdRewardVideoCallbackShow? onAdShow,
      AdCallback? onReward,
      AdCallback? onAdClick,
      AdCallback? onAdClose,
      AdErrorCallback? onError}) async {
    eventChannel.receiveBroadcastStream().listen((event) {
      switch (event["event"]) {
        case "onAdShow":
          onAdShow?.call(event["eCPM"], event["orderNo"]);
          break;
        case "onReward":
          onReward?.call();
          break;
        case "onAdClick":
          onAdClick?.call();
          break;
        case "onAdClose":
          onAdClose?.call();
          break;
        case "onError":
          onError?.call(event["message"]);
          break;
      }
    });
    methodChannel.invokeMethod<String>("loadRewardVideo",
        {"codeId": codeId, "userId": userId, "extra": extra});
  }

  //'1400375964994650194'
  @override
  void loadVideo(String codeId) {
    methodChannel.invokeMethod<String>('loadVideo', {"codeId": codeId});
  }

  @override
  void loadPlaylet(String codeId) {
    methodChannel.invokeMethod<String>('loadPlaylet', {"codeId": codeId});
  }
}
